package com.ocom.common.request.exposeapi.boczhxy;

import javax.crypto.Cipher;
import javax.xml.bind.DatatypeConverter;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

public class RSAService {

	private static final String CHARSET = "GBK";

	public static Map<String, String> genKeyPair() {
		HashMap map = null;

		try {
			map = new HashMap();
			KeyPairGenerator keyPair = KeyPairGenerator.getInstance("RSA");
			keyPair.initialize(1024, new SecureRandom());
			KeyPair kp = keyPair.generateKeyPair();
			String pubKeyStr = bytesToHex(kp.getPublic().getEncoded());
			String priKeyStr = bytesToHex(kp.getPrivate().getEncoded());
			map.put("publicKey", pubKeyStr);
			map.put("privateKey", priKeyStr);
		} catch (Exception var5) {
			var5.printStackTrace();
			map = null;
		}
		return map;
	}

	public static String sign(String data, String privateKey) {
		String sign = null;

		try {
			PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
					hexToBytes(privateKey));
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PrivateKey priKey = keyFactory.generatePrivate(keySpec);
			Signature si = Signature.getInstance("SHA1WithRSA");
			si.initSign(priKey);
			si.update(data.getBytes(CHARSET));
			byte[] dataSign = si.sign();
			sign = bytesToHex(dataSign);
		} catch (Exception var8) {
			var8.printStackTrace();
			sign = null;
		}

		return sign;
	}

	public static boolean verify(String data, String sign, String publicKey) {
		boolean flag = false;
		try {
			Signature si = Signature.getInstance("SHA1WithRSA");
			KeyFactory keyFac = KeyFactory.getInstance("RSA");
			PublicKey puk = keyFac.generatePublic(new X509EncodedKeySpec(
					hexToBytes(publicKey)));
			si.initVerify(puk);
			si.update(data.getBytes(CHARSET));
			flag = si.verify(hexToBytes(sign));
		} catch (Exception var7) {
			var7.printStackTrace();
			flag = false;
		}

		return flag;
	}

	public static String encrypt(String data, String publicKey) {
		String encryptData = null;

		try {
			KeyFactory keyFac = KeyFactory.getInstance("RSA");
			PublicKey pubKey = keyFac.generatePublic(new X509EncodedKeySpec(
					hexToBytes(publicKey)));
			Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(1, pubKey);
			byte[] result = cipher.doFinal(data.getBytes(CHARSET));
			encryptData = bytesToHex(result);
		} catch (Exception var7) {
			var7.printStackTrace();
			encryptData = null;
		}

		return encryptData;
	}

	public static String decrypt(String encryptedData, String privateKey) {
		String decryptData = null;

		try {
			PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
					hexToBytes(privateKey));
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PrivateKey priKey = keyFactory.generatePrivate(keySpec);
			Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(Cipher.DECRYPT_MODE, priKey);
			byte[] result = cipher.doFinal(hexToBytes(encryptedData));
			decryptData = new String(result, CHARSET);
		} catch (Exception var8) {
			var8.printStackTrace();
			decryptData = null;
		}

		return decryptData;
	}

	public static String bytesToHex(byte[] bytes) {
		return DatatypeConverter.printHexBinary(bytes);
	}

	public static byte[] hexToBytes(String hexString) {
		return DatatypeConverter.parseHexBinary(hexString);
	}

}
